﻿Imports System.Windows
Imports System.Windows.Forms
Public Class ListBoxEx
    Public Event GetCompleteWord(ByVal strWord As String)
    Private mToolTip As New ToolTip
    Sub New()
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。
    End Sub
    ''' <summary>
    ''' 确定现在输入的内容是否有可用的候补值
    ''' </summary>
    ''' <param name="strText">单词已经输入部分</param>
    ''' <returns>是否有候补值</returns>
    ''' <remarks></remarks>
    Function IsWordInList(ByVal strText As String) As Boolean
        If strText = "" Then Return False
        If AutoFillWordList.Count = 0 Then Return False
        Me.Items.Clear()
        For Each strKey In AutoFillWordList.Keys
            If InStr(UCase(strKey), UCase(strText)) = 1 And (UCase(strText) <> UCase(strKey)) Then
                Me.Items.Add(strKey)
            End If
        Next
        If Items.Count > 0 Then Return True
    End Function
    Private Sub ListBoxEx_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
        e.DrawBackground()
        e.Graphics.DrawString(Items(e.Index), Me.Font, Drawing.Brushes.Black, e.Bounds.X, e.Bounds.Y)
        If e.State = DrawItemState.Selected Then
            If tblToolTip.ContainsKey(UCase(Items(e.Index))) Then
                mToolTip.Show(Items(e.Index) & vbCrLf & tblToolTip.Item(UCase(Items(e.Index))), Me)
            Else
                mToolTip.Show(Items(e.Index) & vbCrLf & "No ToolTipText", Me)
            End If
        End If
    End Sub
    Private Sub ComboEx_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.Escape
                Me.Visible = False
                mToolTip.RemoveAll()
            Case Keys.Enter, Keys.Space
                Me.Visible = False
                mToolTip.RemoveAll()
                Dim Fillword As String
                If AutoFillWordList.Item(Me.Items(SelectedIndex)) = String.Empty Then
                    Fillword = Me.Items(SelectedIndex)
                Else
                    '为了兼容UNIX，XML里面的换行为LF，需要将换行改为CR/LF!!!
                    Fillword = AutoFillWordList.Item(Me.Items(SelectedIndex)).Replace(vbLf, vbCrLf)
                End If
                RaiseEvent GetCompleteWord(Fillword)
        End Select
    End Sub
End Class
